package com.jeecms.wicool.dao.car.impl;



import java.util.List;

import org.springframework.stereotype.Repository;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.car.CarOrderAppraiseDao;
import com.jeecms.wicool.entity.car.CarOrderAppraise;

@Repository
public class CarOrderAppraiseDaoImpl extends HibernateBaseDao<CarOrderAppraise, Integer>
		implements CarOrderAppraiseDao {

	@Override
	protected Class<CarOrderAppraise> getEntityClass() {
		// TODO Auto-generated method stub
		return CarOrderAppraise.class;
	}
	@Override
	public Pagination getPage(Integer userId,Integer orderId,Short grade,int pageNo, int pageSize,CmsUser user) {
			Finder f = Finder.create("select bean from CarOrderAppraise bean where 1=1 ");
			if (userId!=null){
				f.append(" and bean.user.id = :userId");
				f.setParam("userId",userId);
			}if (orderId!=null){
				f.append(" and bean.order.id = :orderId");
				f.setParam("orderId",orderId);
			}if (grade!=null){
				f.append(" and bean.grade = :grade");
				f.setParam("grade",grade);
			}
			
			if(user.getShopId()!=null){
				f.append(" and bean.shop.id = :shopId");
				f.setParam("shopId",user.getShopId());
			}
			f.append(" order by bean.id desc");
			return find(f, pageNo, pageSize);
	}
	@Override
	public CarOrderAppraise findById(Integer id) {
		CarOrderAppraise entity = get(id);
		return entity;
	}
	@Override
	public CarOrderAppraise findByOrderId(Integer orderId) {
		Finder f = Finder.create("select bean from CarOrderAppraise bean where 1=1 ");
		if (orderId!=null){
			f.append(" and bean.order.id = :orderId");
			f.setParam("orderId",orderId);
		}
		List<CarOrderAppraise> list = find(f);
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}
	@Override
	public CarOrderAppraise deleteById(Integer id) {
		CarOrderAppraise entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
			getSession().flush();
		}
		return entity;
	}
}